﻿
using System;
using System.Collections.Generic;
using System.Text;

using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;
using YouHP.Model.Group;

namespace YouHP.Pages.Group
{
    public class TopicEdit : YouHP.Pages.Group.GroupBasePage
    {
        protected TopicInfo TopicData { get; set; }
        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            HandleCommon();
            if (ErrorCount > 0) return;

            if (IsPost) HandlePost();
            else HandleGet();
        }

        protected new void HandleCommon()
        {
            
            int tid = RequestTools.GetInt("tid",0);
            TopicData = TopicBLL.Instance.GetTopicInfo(tid);
            if (TopicData.Id <= 0)
            {
                this.AddErrLine("该话题不存在");
                return;
            }
          
        }

        protected void HandlePost()
        {
            TopicInfo info = ValidataForm();
            if (info == null) return;
            bool succ=TopicBLL.Instance.UpdateTopic(info);
            if (succ)
            {
                AddMsgLine("修改成功");
                string nexturl = "/Group/Topic.aspx?gid=" + GroupData.Id + "&tid=" + TopicData.Id;
                this.SetUrl(nexturl);
                this.SetMetaRefresh();
            }
            else
            {
                AddErrLine("修改失败");
                return;
            }
        }

        protected void HandleGet()
        {
            //this.AddScriptSrc(TinyMCE.Path);
            //this.AddScript(TinyMCE.InitAdvance("tbody"));

        }

        TopicInfo ValidataForm()
        {
            TopicInfo info = TopicData;
            info.Title = RequestTools.GetFormString("ttitle").Trim();
            info.Body = RequestTools.GetFormString("tbxContent").Trim();
            if (info.Title.Length == 0)
            {
                this.AddErrLine("标题不能空！");
                return null;
            }
            if (info.Title.Length > Config.TitleMaxSize)
            {
                this.AddErrLine("标题长度不能超过"+Config.TitleMaxSize+"字符！");
                return null;
            }

            if (info.Body.Length == 0)
            {
                this.AddErrLine("内容不能为空！");
                return null;
            }
            else if (info.Body.Length > Config.TopicMaxSize)
            {
                this.AddErrLine("内容长度不能大于 " + Config.TopicMaxSize + "字符！");
                return null;
            }
            info.UpdateTime = DateTime.Now;
            return info;
        }
    }
}